package cn.framework.mail.impl;

import java.util.List;
import java.util.Map;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import cn.framework.constant.MailTemplateConsntant;
import cn.framework.mail.SysMailSender;
import cn.framework.util.FreemarkerHelper;
import freemarker.template.Configuration;
import freemarker.template.Template;


@Service
public class SysMailSenderImpl implements SysMailSender {
	
	@Autowired
	JavaMailSender sysJavaMailSender;
	
	@Autowired
	@Qualifier("sysMailSubject")
	String sysMailSubject;
	
	@Autowired
	@Qualifier("sysMailFrom")
	String sysMailFrom;
	
	@Autowired
	@Qualifier("sysMailDisplayName")
	String sysMailDisplayName;
		
	
	public void sendMail(List<String> targetMailList,String content){
		sendMail(targetMailList,null, content);
	}
	
	public void sendMail(List<String> targetMailList,String subject,String content){
			try {
				if(targetMailList!=null && targetMailList.size()>0){
					MimeMessage mimeMessage=sysJavaMailSender.createMimeMessage();
					MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
					InternetAddress from=new InternetAddress(sysMailFrom,sysMailDisplayName);
			        helper.setFrom(from);
			        String[] bcc=new String[targetMailList.size()-1];
			        if(targetMailList!=null){
			        	helper.setTo(targetMailList.get(0));
						targetMailList.remove(0);
			        }else{
			        	return;
			        }
			        
			        bcc=targetMailList.toArray(new String[0]);
			        helper.setBcc(bcc);
			        helper.setSubject(sysMailSubject);
			    	if(subject!=null){
						helper.setSubject(subject);
					}
			        helper.setText(content,true);
					sysJavaMailSender.send(mimeMessage);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
	}
		
	
	public boolean sendMail(String targetMailAddress,String content){
		return sendMail(targetMailAddress, null, content);
	}
	
	public boolean sendMail(String targetMailAddress,String subject,String content){
		try {
			MimeMessage mimeMessage=sysJavaMailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
			InternetAddress from=new InternetAddress(sysMailFrom,sysMailDisplayName);
	        helper.setFrom(from);
	        helper.setTo(targetMailAddress);
	        helper.setSubject(sysMailSubject);
	    	if(subject!=null){
				helper.setSubject(subject);
			}
	        helper.setText(content,true);
			sysJavaMailSender.send(mimeMessage);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
	public boolean sendHTMLMail(List<String> targetMailList,String subject,String templateName,Map model){
		try {
			String content=genHTML(templateName, model);
			sendMail(targetMailList, subject,content);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}		
	}

	public boolean sendHTMLMail(String targetMailAddress,String subject,String templateName,Map model){
		try {
			String content=genHTML(templateName, model);
			sendMail(targetMailAddress, subject,content);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}		
	}
	
	public String genHTML(String templateName,Map model)throws Exception{
		if(!model.containsKey("basePath")){
			throw new Exception("please setup basePath");
		}
		FreemarkerHelper freemarkerHelper=new FreemarkerHelper();
		Configuration configuration=freemarkerHelper.getConfiguration(MailTemplateConsntant.MAIL_TMPLATE_PATH);
		Template t = configuration.getTemplate(templateName);
		String content=FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
		return content;
	}


}
